package cn.edu.xmu.tangzk.scala.s99

import scala.collection.immutable.Nil

object P09 {

    def pack[A](ls: List[A]): List[List[A]] = {
        if (ls.isEmpty) List(List())
        else {
            val (packed, next) = ls span { _ == ls.head }
            if (next == Nil) { List(packed) }
            else { packed :: pack(next) }
        }
    }

    def main(args: Array[String]) {
        println(pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)))
    }
}